#include "Level.h"

void Level::Init()
{
	WallHeight = 20;
	CreateFloor();
}

void Level::Update()
{
	hkVector4 pos = havokRigidBody->getPosition();

	PhysicsFloor::Update();
}

void Level::CreateLevel()
{

}

void Level::CreateGeometry()
{
	CreateFloor();
}

void Level::CreatePhysicsObject()
{
	// create havok object
	havokShape = new hkpListShape(&shapeArray[0], shapeArray.getSize());

	hkpRigidBodyCinfo ci;

	ci.m_shape = havokShape;
	ci.m_friction = 0.2;
	ci.m_motionType = hkpMotion::MOTION_KEYFRAMED;
	ci.m_position = hkVector4(Position.x,Position.y-(dimentions.y/2),Position.z,0);
	ci.m_qualityType = HK_COLLIDABLE_QUALITY_KEYFRAMED;
	ci.m_collisionFilterInfo = hkpGroupFilterSetup::LAYER_KEYFRAME;
	havokRigidBody = new hkpRigidBody( ci ) ;

	havokRigidBody->setName("LEVEL");

}

void Level::CreateFloor()
{
	// D3DShape floor
	GameGeometry* floor = new D3DXCube(dimentions.x,dimentions.y,dimentions.z);
	floor->LocalPosition = D3DXVECTOR3(0,-dimentions.y/2,0);
	floor->SetColor(0.15f,0,0.9f);
	Geometry.push_back(floor);

	// Havok floor shape
	hkVector4 groundRadii( dimentions.x/2, dimentions.y/2, dimentions.z/2 );
	hkpBoxShape *havokfloor = new hkpBoxShape( groundRadii , 0 );

	// now transform it to correct position
	hkTransform t;
	t = t.getIdentity();
	hkVector4 trans = hkVector4(0,-dimentions.y/2,0,0);
	t.setTranslation( trans );
	hkpTransformShape* cube2Trans = new hkpTransformShape(havokfloor, t);
	shapeArray.pushBack(cube2Trans);
}

void Level::CreateWall( float posX, float PosZ, float wallWidth, float wallDepth)
{
	//  D3DShape wall piece

	D3DXVECTOR3 wallSize(wallWidth,WallHeight,wallDepth);
	GameGeometry* wall = new D3DXCube(wallSize.x*0.9f,wallSize.y,wallSize.z*0.9f);
	wall->SetColor(D3DXCOLOR(1,1,1,1));
	wall->LocalPosition = D3DXVECTOR3(posX,wallSize.y/2+0.1f,PosZ);
	Geometry.push_back(wall);

	// havok box shape
	hkVector4 wallRadii(wallSize.x/2,wallSize.y/2,wallSize.z/2);
	hkpBoxShape *Havokwall = new hkpBoxShape(wallRadii,0);

	// now transform it to correct position
	hkTransform t;
	t = t.getIdentity();
	hkVector4 trans = hkVector4(posX,wallSize.y/2,PosZ,0);
	t.setTranslation( trans );
	hkpTransformShape* cube2Trans = new hkpTransformShape(Havokwall,t);
	shapeArray.pushBack(cube2Trans);

}